<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top: 5%; height: 85%; overflow-y: hidden"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" v-bind="layout" :model="model">
        <a-form-model-item label="基地名称" required prop="baseName">
          <a-input
            v-model="model.baseName"
            @change="inputChange"
            placeholder="请输入基地名称"
          />
        </a-form-model-item>
        <a-form-model-item label="基地地址" required prop="address">
          <a-select
            v-model="model.address"
            show-search
            :show-arrow="false"
            label-in-value
            placeholder="请输入基地地址"
            style="width: 100%"
            :filter-option="false"
            @change="handleChange"
            :not-found-content="fetching ? undefined : null"
            :options="baseOptions"
            @search="handChange"
          >
            <template v-if="fetching" #notFoundContent>
              <a-spin size="small" />
            </template>
          </a-select>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { getLocation,addBase,editBase } from '@/api/api'
export default {
  name: 'BaseModal',
  data() {
    return {
      title: '操作',
      fetching: false,
      visible: false,
      disableSubmit: false,
      location:[],   
      baseOptions: [],
      editModel:{},
      model: {},
      spliter: {
        type: String,
        required: false,
        default: ',',
      },
      layout: {
        labelCol: { span: 3 },
        wrapperCol: { span: 14 },
      },
      confirmLoading: false,
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    handleChange(e){
      this.model.address= {value: e.key,label: e.label};
      this.baseOptions =[];
      this.baseOptions.push(this.model.address);
    },
    handChange(e) {
      this.fetching = true
      const params = { keyword: e }
      this.baseOptions=[];
      this.location=[];
      getLocation(params).then((res) => {
        if (res.code === 20000 && res.data.length > 0) {
          this.location=res.data;
          for(var i=0;i<res.data.length;i++){
              const record = { label: res.data[i].adress, value: i}
              this.baseOptions.push(record);
          }
          console.log('location',this.location);
        }
      }) 
      console.log('baseOptions',this.baseOptions);
      
       this.fetching=false;
    },
    inputChange() {
      const params = { keyword: this.model.baseName }
      this.baseOptions=[];
       this.location=[];
      getLocation(params).then((res) => {
        if (res.code === 20000 && res.data.length > 0) {
          this.location=res.data;
          for(var i=0;i<res.data.length;i++){
              const record = { label: res.data[i].adress, value: i}
              this.baseOptions.push(record)
          }
          console.log('location2',this.location);
        }
         
        console.log('baseOptions2',this.baseOptions)

      })
    },
    add() {
       this.visible = true
      //this.edit(this.modelDefault)
    },
    edit(record) {
      console.log('record',record)
      //this.model = Object.assign({}, record)
      this.visible = true
      this.model.baseName=record.shopName;
      this.model.address= { label: record.adress, value: 1};
      this.model.id=record.id
      this.editModel=record;
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
     const params={
        "baseName":that.model.baseName,
        "location":that.location[that.model.address.value]
     }
     let obj
     if(!that.model.id){
        obj = addBase(params);
     }else{
        that.editModel.shopName=this.model.baseName;
        that.editModel.adress=this.model.address.label;
        that.editModel.city=that.location[that.model.address.value] ?that.location[that.model.address.value].pname :that.editModel.city;
        that.editModel.location=that.location[that.model.address.value] ?that.location[that.model.address.value].location :that.editModel.location;
        obj = editBase(that.editModel);
     }
     obj.then((res) => {
        if (res.code === 20000) {
          that.$message.success('添加成功！')
          that.close()
          that.$emit('ok')
        } else {
          that.$message.warning('添加失败！')
          that.close()
        }
      })

     

    },
    handleCancel() {
      this.close()
    },
  },
}
</script>

<style scoped>
</style>